import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:window_manager/window_manager.dart';

import '../app_utils.dart';

class WindowConfig {
  static Future<void> init() async {
    if (!AppUtils.isWeb) {
      WidgetsFlutterBinding.ensureInitialized();
      // 必须加上这一行。
      await windowManager.ensureInitialized();
      // 配置窗口
      WindowOptions windowOptions = const WindowOptions(
        size: Size(1200, 800),
        minimumSize: Size(960, 640),
        center: true,
        backgroundColor: Colors.transparent,
        skipTaskbar: false,
        titleBarStyle: TitleBarStyle.hidden, // 去除顶部标题栏
      );
      windowManager.waitUntilReadyToShow(windowOptions, () async {
        //去除边框，这样就不能调节大小了
        await windowManager.setAsFrameless();
        await windowManager.show();
        await windowManager.focus();
      });
    }
  }

  ///拖拽移动窗口
  static dragging({Widget? child}) {
    if (AppUtils.isWeb) {
      return SizedBox(child: child,);
    }
    return GestureDetector(
      onPanStart: (details) {
        if (!AppUtils.isWeb) {
          windowManager.startDragging();
        }
      },
      child: child,
    );
  }
}

/// 调节窗口大小
class WindowStartResizing extends StatelessWidget {
  Widget? child;

  WindowStartResizing({super.key, this.child});

  @override
  Widget build(BuildContext context) {
    if (AppUtils.isWeb) {
      return const SizedBox(
        width: 0,
        height: 0,
      );
    }
    return GestureDetector(
      onPanStart: (details) {
        // 检测鼠标位置并设置窗口调整模式
        if (_isOnEdge(details.localPosition, context)) {
          windowManager.startResizing(ResizeEdge.right);
        }
      },
      onPanUpdate: (details) {
        // 实现窗口调整逻辑
      },
      onPanEnd: (_) {},
      child: child,
    );
  }

  bool _isOnEdge(Offset localPosition, BuildContext context) {
    // 根据需要定义边缘检测逻辑
    // 这里只是一个简单的例子
    return localPosition.dx < 10 || localPosition.dy < 10 || localPosition.dx > MediaQuery.of(context).size.width - 10 || localPosition.dy > MediaQuery.of(context).size.height - 10;
  }
}

/// 固定大小拖拽区域
class DragAreaPositioned extends StatelessWidget {
  const DragAreaPositioned({super.key});

  @override
  Widget build(BuildContext context) {
    if (AppUtils.isWeb) {
      return const SizedBox(
        width: 0,
        height: 0,
      );
    }
    return const Positioned(
        top: 0,
        left: 0,
        right: 0,
        height: 20,
        // 设置为20像素高
        child: DragArea(
          height: 20,
        ));
  }
}

/// 拖拽区域
class DragArea extends StatelessWidget {
  final double height;

  const DragArea({Key? key, this.height = 20}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (AppUtils.isWeb) {
      return const SizedBox(
        width: 0,
        height: 0,
      );
    }
    return GestureDetector(
      onPanStart: (details) {
        if (!AppUtils.isWeb) {
          windowManager.startDragging();
        }
      },
      child: Container(
        height: height, // 确保与 Positioned 的高度一致
        color: Colors.transparent, // 半透明背景以标识拖动区域
      ),
    );
  }
}
